#include <REGX52.H>
#include <INTRINS.H>
#include "delay.h"
#include "DS1302.h"
#include "LCD1602.h"
#include "Key.h"
#include "Timer0.h"
#include "UART.H"

unsigned char KeyNum = 0;
unsigned char Mode = 0;
unsigned char TimeSetSelect = 0;
unsigned char TimeFlashFlag = 0;
	
void TimeShow(void)
{
	DS1302_ReadTime();
	
	LCD_ShowString(1, 1, "  -  -");
	LCD_ShowNum(1, 1, DS1302_Time[0], 2);
	LCD_ShowNum(1, 4, DS1302_Time[1], 2);
	LCD_ShowNum(1, 7, DS1302_Time[2], 2);


	LCD_ShowString(2, 1, "  :  :");
	LCD_ShowNum(2, 1, DS1302_Time[3], 2);
	LCD_ShowNum(2, 4, DS1302_Time[4], 2);
	LCD_ShowNum(2, 7, DS1302_Time[5], 2);
	

}

void TimeSet(void)
{
	if(KeyNum == 2)
	{
		TimeSetSelect++;
		TimeSetSelect %= 6;
	}
	else if(KeyNum == 3)
	{
		DS1302_Time[TimeSetSelect]++;
		
		if(DS1302_Time[0] > 99) { DS1302_Time[0] = 0;}
		if(DS1302_Time[1] > 12) { DS1302_Time[1] = 1;}
		
		if(DS1302_Time[1] == 1 || DS1302_Time[1] == 3 || DS1302_Time[1] == 5 || DS1302_Time[1] == 7 
			|| DS1302_Time[1] == 8 || DS1302_Time[1] == 10 ||  DS1302_Time[1] == 12)
		{
			if(DS1302_Time[2] > 31) { DS1302_Time[2] = 1;}
		}
		else if(DS1302_Time[1] == 4 || DS1302_Time[1] == 6 || DS1302_Time[1] == 9 || DS1302_Time[1] == 11)
		{
			if(DS1302_Time[2] > 30) { DS1302_Time[2] = 1;}
		}
		else if(DS1302_Time[1] == 2)
		{
			if((DS1302_Time[0] % 4) == 0)
				if(DS1302_Time[2] > 29) { DS1302_Time[2] = 1;}
			else
				if(DS1302_Time[2] > 29) { DS1302_Time[2] = 1;}
		}
		
		if(DS1302_Time[3] > 23) { DS1302_Time[3] = 0;}
		if(DS1302_Time[4] > 59) { DS1302_Time[4] = 0;}
		if(DS1302_Time[5] > 59) { DS1302_Time[5] = 0;}
	}
	else if(KeyNum == 4)
	{
		DS1302_Time[TimeSetSelect]--;
		
		
		if(DS1302_Time[0] < 0) { DS1302_Time[0] = 99;}
		if(DS1302_Time[1] < 1) { DS1302_Time[1] = 12;}
		
		if(DS1302_Time[1] == 1 || DS1302_Time[1] == 3 || DS1302_Time[1] == 5 || DS1302_Time[1] == 7 
			|| DS1302_Time[1] == 8 || DS1302_Time[1] == 10 ||  DS1302_Time[1] == 12)
		{
			if(DS1302_Time[2] < 1) { DS1302_Time[2] = 31;}
			if(DS1302_Time[2] > 31) { DS1302_Time[2] = 1;}
			
		}
		else if(DS1302_Time[1] == 4 || DS1302_Time[1] == 6 || DS1302_Time[1] == 9 || DS1302_Time[1] == 11)
		{
			if(DS1302_Time[2] < 1) { DS1302_Time[2] = 30;}
			if(DS1302_Time[2] > 30) { DS1302_Time[2] = 1;}
		}
		else if(DS1302_Time[1] == 2)
		{
			if((DS1302_Time[0] % 4) == 0){
				if(DS1302_Time[2] < 1) { DS1302_Time[2] = 29;}
				if(DS1302_Time[2] > 29) { DS1302_Time[2] = 1;}
			}
			else{
				if(DS1302_Time[2] < 1) { DS1302_Time[2] = 28;}
				if(DS1302_Time[2] > 28) { DS1302_Time[2] = 1;}
			}
		}
		
		if(DS1302_Time[3] < 0) { DS1302_Time[3] = 23;}
		if(DS1302_Time[4] < 0) { DS1302_Time[4] = 59;}
		if(DS1302_Time[5] < 0) { DS1302_Time[5] = 59;}
	}
	
	
	//if(KeyNum)
	{
		if(TimeSetSelect==0 && TimeFlashFlag==1)
		{
			LCD_ShowString(1, 1, "  ");
		}
		else{
			LCD_ShowNum(1, 1, DS1302_Time[0], 2);
		}

		if(TimeSetSelect==1 && TimeFlashFlag==1)
		{
			LCD_ShowString(1, 4, "  ");
		}
		else{
			LCD_ShowNum(1, 4, DS1302_Time[1], 2);
		}

		if(TimeSetSelect==2 && TimeFlashFlag==1)
		{
			LCD_ShowString(1, 7, "  ");
		}
		else{
			LCD_ShowNum(1, 7, DS1302_Time[2], 2);
		}

		if(TimeSetSelect==3 && TimeFlashFlag==1)
		{
			LCD_ShowString(2, 1, "  ");
		}
		else{
			LCD_ShowNum(2, 1, DS1302_Time[3], 2);
		}

		if(TimeSetSelect==4 && TimeFlashFlag==1)
		{
			LCD_ShowString(2, 4, "  ");
		}
		else{
			LCD_ShowNum(2, 4, DS1302_Time[4], 2);
		}

		if(TimeSetSelect==5 && TimeFlashFlag==1)
		{
			LCD_ShowString(2, 7, "  ");
		}
		else{
			LCD_ShowNum(2, 7, DS1302_Time[5], 2);
		}

		LCD_ShowNum(2, 13, TimeFlashFlag, 1);
		if(KeyNum)
			LCD_ShowNum(2, 10, KeyNum, 1);
	}	
}

void main()
{
	DS1302_Init();
	LCD_Init();
	Timer0_Init();
	
	DS1302_SetTime();
	
	while(1)
	{
		KeyNum = Key();
		if(KeyNum)
		{
			if(KeyNum == 1)
			{
				if(Mode == 0) {Mode=1; }
				else if(Mode == 1)
				{	Mode=0; 
					DS1302_SetTime();
					TimeSetSelect = 0;
				}
				
			}			
		}
		
		switch(Mode)
		{
			case 0: TimeShow(); break;
			case 1: TimeSet();  break;
		}
		
		//Delay(1000);
	}
}


/**
  * @brief  定时器0中断处理函数模版
  * @param  无
  * @retval 无
  */
void Timer_Routine(void) interrupt 1
{
	static unsigned int count = 0;
	
	count++;
	if(count >= 500)		//定时器T0每1ms中断一次，进入1000次经过了1s
	{
		TimeFlashFlag = !TimeFlashFlag;
		count = 0;
	}
	
	//定时器溢出之后需要重新装载
	TH0 = (65535 - 1000) / 256;					//12MHz晶振，12分频
	TL0 = (65535 - 1000) % 256 + 1; 			//
}
